package com.hyg.oa.contract.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hyg.oa.common.DateUtil;
import com.hyg.oa.common.PageUtils;
import com.hyg.oa.common.Pagination;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hyg.oa.common.UUIDUtils;
import com.hyg.oa.contract.model.Contract;
import com.hyg.oa.contract.service.ContractService;


/**
 * Controller类
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/contract") 
public class ContractController {
	
	@Autowired
	private ContractService contractService;
	
	/**
	 * page
	 * @return
	 */
	@RequestMapping("/page")
	public ModelAndView page(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		//总数
		long totalCount = this.contractService.findTotalCount();
		//page对象
		Pagination pagination = PageUtils.create(request, totalCount);
		
		//数据
		List<Contract> list = this.contractService.findPage(pagination.getPageNo(), pagination.getPageSize());
		model.put("list", list);
		model.put("pagination", pagination);
		
		return new ModelAndView("/jsp/contract/list.jsp", model);
	}
	
	
	/**
	 * 列表
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list() {
		Map<String, Object> model = new HashMap<String,Object>();
		
		List<Contract> list = this.contractService.findAll();
		model.put("list", list);
		
		return new ModelAndView("/jsp/contract/list.jsp", model);
	}
	
	/**
	 * 新增
	 * @return
	 */
	@RequestMapping("/add")
	public ModelAndView add() {
		return new ModelAndView("/jsp/contract/add.jsp");
	}
	
	
	
	/**
	 * 保存
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Object save(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String username = request.getParameter("username");
			String number = request.getParameter("number");
			String type = request.getParameter("type");
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String validTime = request.getParameter("validTime");
			String istry = request.getParameter("istry");
			String iscontinue = request.getParameter("iscontinue");
			String content = request.getParameter("content");
			String remark = request.getParameter("remark");
			
			if(StringUtils.isEmpty(istry)) {
				istry = "0";
			}
			if(StringUtils.isEmpty(iscontinue)) {
				iscontinue = "0";
			}
		
			Contract contract = new Contract();
			contract.setCreatetime(new Date());
			contract.setUsername(username);
			contract.setNumber(number);
			contract.setType(type);
			contract.setStartTime(DateUtil.toDate(startTime, "yyyy-MM-dd"));
			contract.setEndTime(DateUtil.toDate(endTime, "yyyy-MM-dd"));
			contract.setValidTime(DateUtil.toDate(validTime, "yyyy-MM-dd"));
			contract.setIstry(Integer.parseInt(istry));
			contract.setIscontinue(Integer.parseInt(iscontinue));
			contract.setContent(content);
			contract.setRemark(remark);
			contract.setId(UUIDUtils.generate());
			this.contractService.save(contract);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}
	
	
	/**
	 * 用户编辑
	 * @return
	 */
	@RequestMapping("/edit")
	public ModelAndView edit(HttpServletRequest request) {
		Map<String, Object> model = new HashMap<String,Object>();
		
		String id = request.getParameter("id");
		Contract contract = this.contractService.findById(id);
		model.put("contract", contract);
		
		return new ModelAndView("/jsp/contract/edit.jsp", model);
	}
	
	
	/**
	 * 更新
	 * @return
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Object update(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String id = request.getParameter("id");
			String username = request.getParameter("username");
			String number = request.getParameter("number");
			String type = request.getParameter("type");
			String startTime = request.getParameter("startTime");
			String endTime = request.getParameter("endTime");
			String validTime = request.getParameter("validTime");
			String istry = request.getParameter("istry");
			String iscontinue = request.getParameter("iscontinue");
			String content = request.getParameter("content");
			String remark = request.getParameter("remark");
			
			if(StringUtils.isEmpty(istry)) {
				istry = "0";
			}
			if(StringUtils.isEmpty(iscontinue)) {
				iscontinue = "0";
			}
		
			
			Contract contract = this.contractService.findById(id);
			contract.setUpdatetime(new Date());
			contract.setUsername(username);
			contract.setNumber(number);
			contract.setType(type);
			contract.setStartTime(DateUtil.toDate(startTime, "yyyy-MM-dd"));
			contract.setEndTime(DateUtil.toDate(endTime, "yyyy-MM-dd"));
			contract.setValidTime(DateUtil.toDate(validTime, "yyyy-MM-dd"));
			contract.setIstry(Integer.parseInt(istry));
			contract.setIscontinue(Integer.parseInt(iscontinue));
			contract.setContent(content);
			contract.setRemark(remark);
			this.contractService.update(contract);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}

	
	/**
	 * 删除
	 * @return
	 */
	@RequestMapping("/del")
	@ResponseBody
	public Object del(HttpServletRequest request) {
		
		Map<String,Object> result = new HashMap<String,Object>();
		
		try {
			String id = request.getParameter("id");
			this.contractService.delete(id);
			
			result.put("success", true);
			
		} catch (Exception e) {
			result.put("success", false);
			e.printStackTrace();
		}
		
		return result;
	}
}
